clear all
set more off, perm
local mydir "`1'"
global Tables	`mydir'/XS/Tables
global Work	`mydir'/XS/DataWork
global Raw	`mydir'/XS/DataRaw
global WRDS	`mydir'/WRDS
* *******************************************
set varabbrev off
* *******************************************
* Get market return (use sft to download CRSP data from WRDS)
import sas $WRDS/CRSP/msi.sas7bdat, clear
rename DATE date
format date %td

generate datem=mofd(date)
format datem %tm
keep datem vwretd

tempfile crsp
save "`crsp'", replace
* ****************************************************** 
* Get data on betting-agaist-beta from AQR (https://www.aqr.com/Insights/Datasets/Betting-Against-Beta-Equity-Factors-Monthly)
import excel "$Raw/Betting Against Beta Equity Factors Monthly.xlsx", /*
*/	sheet("BAB Factors") cellrange(A19:AE1106) firstrow clear

rename  USA BAB
replace BAB=BAB*100

keep DATE BAB

tempfile aqr
save "`aqr'", replace
* ****************** 
* Get data on Quality-Minus-Junk factor from AQR 
* (https://www.aqr.com/Insights/Datasets/Quality-Minus-Junk-Six-Portfolios-Formed-on-Size-and-Quality-Monthly)
import excel "$Raw/Quality Minus Junk Six Portfolios Formed on Size and Quality Monthly.xlsx", /*
*/	sheet("Size x Quality (2 x3)") cellrange(A19:H787) firstrow clear
gen QMJ=100*Factor

keep DATE QMJ

merge 1:1 DATE using "`aqr'"
drop _merge

gen datem=mofd(DATE)
format datem %tm
drop DATE

save "`aqr'", replace
* ****************************************************** 
* WORK WITH FF DATA
* ****************************************************** 
* Get data on Fama-French 5 factors from French's website (https://mba.tuck.dartmouth.edu/pages/faculty/ken.french/data_library.html)
* 5 FF factors 
import delimited $Raw/F-F_Research_Data_5_Factors_2x3.CSV, rowrange(4:700)  varnames(4) clear 
rename *, upper
rename V1 date

tempfile ken
save "`ken'", replace
* ******************
* Get data on dividend-to-price factor from French's website (https://mba.tuck.dartmouth.edu/pages/faculty/ken.french/data_library.html)
import delimited $Raw/6_Portfolios_ME_DP_2x3.CSV , varnames(20) rowrange(20:1148)  clear 
rename v1   date

gen dpHML= (smallhidp+bighidp)/2-(smalllodp+biglodp)/2
keep date dpHML

merge 1:1 date using "`ken'"
drop if _merge==2
drop _merge
save "`ken'", replace
* **************************************
* merge with AQR
gen yr=int(date/100)
gen mo=date-100*yr
order yr mo

generate mdy=mdy(mo+1,1,yr)-1 	if mo<12
replace  mdy=mdy(12,31,yr)	if mo==12
format mdy %td
order mdy date

gen datem=mofd(mdy)
format datem %tm
drop yr mo date
order datem mdy

keep if mdy>=mdy(6,1,1963)

merge 1:1 datem using "`aqr'"
drop if _merge==2
drop _merge
* ******************
* add market
merge 1:1 datem using "`crsp'"
drop if _merge==2
drop _merge
tsset datem
generate MKT=100*vwretd

foreach var of varlist MKT MKTRF RF SMB HML RMW CMA dpHML BAB QMJ {
	quietly: gen `var'Mo1=F.`var'

	quietly: gen `var'Yr1=1
	quietly: gen `var'Yr3=1
	quietly: gen `var'Yr5=1
	
	forvalues x=1(1)12 {
		quietly: replace `var'Yr1=(1+F`x'.`var'/100)*`var'Yr1
	}
	forvalues x=1(1)36 {
		quietly: replace `var'Yr3=(1+F`x'.`var'/100)*`var'Yr3
	}
	forvalues x=1(1)60 {
		quietly: replace `var'Yr5=(1+F`x'.`var'/100)*`var'Yr5
	}
}
foreach var of varlist *Yr1 *Yr3 *Yr5 {
	quietly: replace `var'=(`var'-1)*100
}
keep datem mdy *1 *3 *5
* *****************************************************
tempfile kenaqr
save "`kenaqr'", replace
* *****************************************************
* compute returns for LTG portfolios (use file created by LTG_BM.do)
use permco permno datem LTG LTGALL size ret1 F0Mcap if LTG+size<. using $Work/RET_LTG, clear

tsset permco datem
gen FwdDLTG=F12.LTG-LTG

collapse (count) obs=ret1 (mean) ret1 FwdDLTG [aw=F0Mcap], by(LTGALL datem)
replace ret1=. 		if obs==0
replace FwdDLTG=. 	if obs==0
drop obs
* **********************************************
rename  ret1 ret
reshape wide ret FwdDLTG,i(datem) j(LTGALL)
* **********************************************
gen FwdDLTG=FwdDLTG10-FwdDLTG1
drop FwdDLTG*0 FwdDLTG*1

gen LTGHiMo1 =1+ret10
gen LTGLoMo1 =1+ret1
gen LTGHMLMo1=LTGHiMo1-LTGLoMo1+1

* compute yearly returns
gen LTGLoYr1=1+ret1
gen LTGLoYr3=1+ret1
gen LTGLoYr5=1+ret1

gen LTGHiYr1=1+ret10
gen LTGHiYr3=1+ret10
gen LTGHiYr5=1+ret10

tsset datem	
forvalues x=1(1)11 {
	replace LTGHiYr1 =(F`x'.LTGHiMo1 )*LTGHiYr1
	replace LTGLoYr1 =(F`x'.LTGLoMo1 )*LTGLoYr1
}
forvalues x=1(1)35 {
	replace LTGHiYr3 =(F`x'.LTGHiMo1 )*LTGHiYr3
	replace LTGLoYr3 =(F`x'.LTGLoMo1 )*LTGLoYr3
}
forvalues x=1(1)59 {
	replace LTGHiYr5 =(F`x'.LTGHiMo1 )*LTGHiYr5
	replace LTGLoYr5 =(F`x'.LTGLoMo1 )*LTGLoYr5
}	
foreach var of varlist *Mo1 *Yr1 *Yr3 *Yr5 {
	replace `var'=(`var'-1)*100
}
generate LTGHMLYr1=LTGHiYr1-LTGLoYr1
generate LTGHMLYr3=LTGHiYr3-LTGLoYr3
generate LTGHMLYr5=LTGHiYr5-LTGLoYr5

tsset datem
twoway (tsline LTGHMLYr1) (tsline LTGHMLMo1)
keep datem *1 *3 *5 FwdDLTG
* **********************************************
merge 1:1 datem using "`kenaqr'"
drop _merge
save $Work/FF01, replace
